package com.huangpuweb.api.service.imp;

import com.huangpuweb.api.model.SysHpPark;
import com.huangpuweb.api.model.TblMetroInout;
import com.huangpuweb.api.service.MetroService;
import com.huangpuweb.api.util.StringUtil;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 地铁相关
 */
@Service
public class MetroServiceImpl extends  BaseSTDataServiceImp implements MetroService {

    @Override
    public TblMetroInout getTblMetroInout(TblMetroInout tblMetroInout){
        String sql = "select stmt_day, station_nm_cn, sum(enter_times) inSum, sum(exit_times) outSum from tbl_metro_inout where stmt_day = ? ";
        List list = new ArrayList();
        list.add(tblMetroInout.getStmtDay());
        if(!StringUtil.isEmpty(tblMetroInout.getStationNmCn())){
            sql += " and station_nm_cn = ?";
            list.add(tblMetroInout.getStationNmCn());
        }

        return getEntry( sql, new RowMapper<TblMetroInout>(){
            @Override
            public TblMetroInout mapRow(ResultSet resultSet, int i) throws SQLException {
                TblMetroInout tblMetroInout = new TblMetroInout();
                tblMetroInout.setStmtDay(resultSet.getString("stmt_day"));
                tblMetroInout.setStationNmCn(resultSet.getString("station_nm_cn"));
                tblMetroInout.setInSum(resultSet.getInt("inSum"));
                tblMetroInout.setOutSum(resultSet.getInt("outSum"));
                return tblMetroInout;
            }
        }, list.toArray());
    }

    /**
     * 获取最新数据
     * @param tblMetroInout
     * @return
     */
    @Override
    public TblMetroInout getFiveMinuteOTblMetroInout(TblMetroInout tblMetroInout){
        String sql = "select stmt_day, station_nm_cn, sum(enter_times) fiveMinuteInSum, sum(exit_times) fiveMinuteOutSum from tbl_metro_inout where stmt_day = ?" +
                " and station_nm_cn = ? " +
                " group by dsjzx_create_time order by dsjzx_create_time desc  limit 1;";
        List list = new ArrayList();
        list.add(tblMetroInout.getStmtDay());
        list.add(tblMetroInout.getStationNmCn());
        return getEntry( sql, new RowMapper<TblMetroInout>(){
            @Override
            public TblMetroInout mapRow(ResultSet resultSet, int i) throws SQLException {
                TblMetroInout tblMetroInout = new TblMetroInout();
                tblMetroInout.setStmtDay(resultSet.getString("stmt_day"));
                tblMetroInout.setStationNmCn(resultSet.getString("station_nm_cn"));
                tblMetroInout.setFiveMinuteInSum(resultSet.getInt("fiveMinuteInSum"));
                tblMetroInout.setFiveMinuteOutSum(resultSet.getInt("fiveMinuteOutSum"));
                return tblMetroInout;
            }
        }, list.toArray());
    }

    @Override
    public List<TblMetroInout> getTblMetroInoutList(TblMetroInout tblMetroInout){
        String sql = "select stmt_day, station_nm_cn, sum(enter_times) inSum, sum(exit_times) outSum from tbl_metro_inout where stmt_day = ? group by station_nm_cn;";
        List list = new ArrayList();
        list.add(tblMetroInout.getStmtDay());

        return getEntryList( sql, new RowMapper<TblMetroInout>(){
            @Override
            public TblMetroInout mapRow(ResultSet resultSet, int i) throws SQLException {
                TblMetroInout tblMetroInout = new TblMetroInout();
                tblMetroInout.setStmtDay(resultSet.getString("stmt_day"));
                tblMetroInout.setStationNmCn(resultSet.getString("station_nm_cn"));
                tblMetroInout.setInSum(resultSet.getInt("inSum"));
                tblMetroInout.setOutSum(resultSet.getInt("outSum"));
                return tblMetroInout;
            }
        }, list.toArray());
    }
}
